Ext.define('DragonApp.util.Util', {

  statics : {
    /**
     * [decodeJSON description] json解码
     *
     * @param    {string} text
     * @return   {object}
     */
    decodeJSON : function (text) {
      var result = Ext.JSON.decode(text, true);
      if (!result){
        result = {};
        result.success = false;
        result.msg = text;
      }
      return result;
    },
    /**
     * [handleFormFailure description] form提交异常响应
     *
     * @param    {object} action 返回
     * @return   void
     */
    handleFormFailure: function(action){

        var me = this,
            result = me.decodeJSON(action.response.responseText);

        switch (action.failureType) {
            case Ext.form.action.Action.CLIENT_INVALID:
                DragonApp.util.Msg.showErrorMsg('Form fields may not be submitted with invalid values');
                break;
            case Ext.form.action.Action.CONNECT_FAILURE:
                DragonApp.util.Msg.showErrorMsg(action.response.responseText);
                break;
            case Ext.form.action.Action.SERVER_INVALID:
                DragonApp.util.Msg.showErrorMsg(result.msg);
        }
    },
    /**
     * [copyObj description]  对象深拷贝
     *
     * @param    {object} record  被拷贝对象
     * @return   {object}
     */
    copyObj : function (record) {

      return JSON.parse(JSON.stringify(record));
    },

    /**
     * [addSign description]  数字签名
     *
     * @param    {array} data 字段数组
     */
    addSign: function (data){

      var str = '';
      var arr = [];

      data.app_id = DragonApp.ux.config.Config.getAppId();
      arr = DragonApp.util.Object.orderArray(data);

      for( var x in arr ){
        str += ( x + '=' + arr[x]+'&');
      }

      str = str.substring(0, str.length-1);
      str = str + DragonApp.ux.config.Config.getAppSecret();

      data.sign = CryptoJS.MD5(str).toString();
      return data;
    },

    /**
     * [setCookie description]  Cookie设置
     *
     * @param    {array} datas 键值对数组
     * @param    void
     */
    setCookie: function (datas,days) {

      var now = new Date();
      var expires = new Date(now.getTime() + days * 24 * 60 * 60 * 1000);

      Ext.util.Cookies.set('is_setCookie', 'true', expires);

      datas.forEach(function(item, index) {
        var name  = item.getName();
        var value = item.getValue().trim();
        Ext.util.Cookies.set( name, value, expires);
      });

    },

    /**
     * [clearCookie description]  Cookie清除
     *
     * @param    {array} items  字段数组
     * @return   void
     */
    clearCookie: function (items) {

      items.forEach(function(item, index) {
          Ext.util.Cookies.clear(item);
      });
    },
    /**
     * 数据仓库匹配字段转换
     * @example
     * var param = [
                {'store':Ext.create('DragonApp.store.Quality'),'field_s':'quality','field_m':'id','field_v':'name'},
                {'store':Ext.create('DragonApp.store.MonsterLocation'),'field_s':'location','field_m':'id','field_v':'name'}];

       var myFun = DragonApp.util.Util.transferMatchFieldOfStore(param);

     * @return   {object}  函数
     */
    transStore: function(storeArray)
    {
        var _param = storeArray;

        //返回函数访问私有变量
         return function ( me , records , successful , operation , eOpts ) {

            if (successful == true) {

                var ori_records = records;

                var _arr = _param;

                for(var i= 0;i<_arr.length;i++){
                    var item = _arr[i];
                    var matchStore = item.store;
                    var sourceField = item.field_s;
                    var matchField = item.field_m;
                    var valueField = item.field_v;

                    !function(matchStore,sourceField,matchField,valueField){

                        //============
                        //遍历数组加载,
                        //匹配仓库
                        //============
                        matchStore.load({

                            callback: function(records, operation, success) {
                              if (false === success) {
                                  return false;
                              } else if (true === success) {

                                  for (var _i = 0; _i < ori_records.length; _i++) { //遍历源始仓库

                                      var hasMatch = false;
                                      var finalValue = null;
                                      var record = ori_records[_i];

                                      for (var _j = 0; _j < records.length; _j++) { //遍历匹配仓库
                                      var matchRecord = records[_j];

                                      //寻找匹配记录
                                      if (record.get(sourceField) == matchRecord.get(matchField)) {
                                        finalValue = matchRecord.get(valueField);
                                        hasMatch = true;
                                        break;
                                      }
                                      }
                                      if(true===hasMatch){
                                      record.set(sourceField, finalValue);  //匹配字段赋值
                                      }
                                  }
                                  me.commitChanges();
                               }
                            }
                        });
                    }(matchStore,sourceField,matchField,valueField);
                }
            }
        };
    }
  }
});